<template>
  <basic-container :btnFlag="true">
    <div class="card-border">
      <card-header :header-data="headerData" @buttonClick="handleAdd()"></card-header>
      <div class="detail-group">
        <div
          class="detail-data-line"
          :class="{'detail-data-inline':item.newline}"
          v-for="(item, index) in detailData"
          :key="index"
        >
          <label class="detail-data-label">{{item.label}}:</label>
          <div
            class="detail-data-value"
            :class="{'detail-data-value-big':!item.newline}"
            v-if="item.value instanceof (Array)"
          >
            <div style="font-size:14px;color:#606266">
              <div v-for="(file,index) in item.value" :key="index">
                <el-tooltip class="item" effect="dark" content="下载资料" placement="top-start">
                  <el-button class="downbtn" @click="downloadFile(file.fileId,file.fileName)">
                    <img src="img/download-blue.png" class="detail-data-img" />
                    <span>下载</span>
                  </el-button>
                </el-tooltip>
                <p class="detail-data-file" v-if="file.fileName">{{file.fileName}}</p>
              </div>
              <p class="detail-data-file" v-if="item.value.length==0">无</p>
            </div>
          </div>
          <p
            class="detail-data-value"
            :class="{'detail-data-value-big':!item.newline}"
            v-else-if="item.label=='方案名称'"
          >
            <span
              style="cursor: pointer;"
              @click="watchDetail(item)"
              :class="{'detail-data-color-warning':item.colorType=='warning'}"
            >{{item.value}}</span>
          </p>

          <p
            class="detail-data-value"
            :class="{'detail-data-value-big':!item.newline}"
            v-else-if="item.value"
          >
            <span :class="{'detail-data-color-warning':item.colorType=='warning'}">{{item.value}}</span>
          </p>
        </div>
      </div>
    </div>
  </basic-container>
</template>
<script>
import { getReportDetail } from "@/api/pim/pim";
import { getunreadcontent } from "@/api/system";
import { filedownload } from "@/api/system";
import { getNotNull, getFileTypeFiles } from "@/views/util";
import { SYS_DIC } from "@/const/dict";
export default {
  name: "application",
  mounted() {
    this.getMessageList();
    if (this.$route.query.businessKey) {
      this.getReport();
    }
  },
  data() {
    return {
      headerData: {
        title: "消息详情"
      },
      detailData: []
    };
  },
  methods: {
    watchDetail() {
      let src = "/modifyProgram/detail";
      this.$router.push({
        path: src,
        query: {
          watch: true,
          draftId: this.businessKey
        }
      });
    },

    // 附件下载
    downloadFile(fileId, fileName) {
      filedownload(fileId)
        .then(response => {
          if (response.status == 200) {
            this.download(response.data, fileName);
          }
        })
        .catch(error => {
          this.$notify({
            title: "失败",
            message: "上传失败[" + error + "]",
            type: "error",
            duration: 2000
          });
        });
    },
    // 下载文件
    download(data, filename) {
      if (data == null) {
      } else {
        let url = window.URL.createObjectURL(new Blob([data]));
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.click();
      }
    },
    getReport() {
      getReportDetail(this.$route.query.businessKey).then(async response => {
        if (response.data.code == "0") {
          let d = response.data.data;
          let data = [
            {
              label: "报告",
              value: d.fileIds ? await getFileTypeFiles(d.fileIds) : "无"
            },
            {
              label: "附件",
              value: d.material_fileIds
                ? await getFileTypeFiles(d.material_fileIds)
                : "无"
            }
          ];
          if (d.reportType !== "1") {
            data.unshift({
              label: "上传时间",
              value: d.createTime ? d.createTime.slice(0, 7) : "无"
            });
          }
          data.unshift({
            label: "报告名称",
            value:
              d.reportType == "1"
                ? d.title + d.year + "年度监测分析报告"
                : d.reportType == "2"
                ? d.title + "中期评估报告"
                : d.reportType == "3"
                ? d.title + "期末总结报告"
                : ""
          });
          this.detailData.push(...data);
        }
      });
    },
    getMessageList() {
      getunreadcontent(this.$route.params.id).then(response => {
        if (response.data.code == "0") {
          var records = response.data.data;
          this.businessKey=records.businessKey;
          this.detailData = [
            // {label:'发送者',value:getNotNull(records.senderId),newline:true},
            { label: "发送时间", value: getNotNull(records.sendTime) },
            // {label:'接收者',value:getNotNull(records.recipientId),newline:true},
            { label: "接收时间", value: getNotNull(records.readTime) },
            { label: "消息内容", value: getNotNull(records.content) },
            {
              label: "状态",
              value: getNotNull(records.haveRead, SYS_DIC.MSGREAD)
            }
          ];
          if (this.$route.query.release) {
            this.detailData.push({
              label: "方案名称",
              value: records.formKey + "责任分解方案"
            });
          }
          if (records.type == "1") {
            this.detailData.splice(2, 1);
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.card-border {
  border: 1px solid #d2d2d2;
}
.card-body {
  padding: 10px;
}
.detail-group {
  padding: 20px;
}
.detail-data-inline {
  display: inline-block;
}
.detail-data-line {
  font-size: 1rem;
}
.detail-data-label {
  display: inline-block;
  width: 140px;
  font-weight: 600;
}
.detail-data-value {
  display: inline-block;
  margin-right: 12px;
  width: 200px;
}
.detail-data-value-big {
  width: 80%;
}
.detail-data-file {
  display: inline-block;
  margin-left: 12px;
}
.detail-data-img {
  width: 23px;
  height: 23px;
  position: relative;
  top: 6px;
}
.detail-data-color-warning {
  background-color: #e6a23c;
  color: #fff;
  padding: 2px;
}
</style>
